<?php
class ZSystem
{
	static public function GetINI() {
		global $INI;
		$INI = Config::Instance('php');
		$SYS = Table::Fetch('system', 1);
		$SYS = Utility::ExtraDecode($SYS['value']);
		$INI = Config::MergeINI($INI, $SYS);
		$INI = ZSystem::WebRoot();
		return self::BuildINI($INI);
	}

	static public function GetUnsetINI($ini) {
		unset($ini['db']);
		unset($ini['webroot']);
		unset($ini['memcache']);
		return $ini;
	}

	static public function GetSaveINI($ini) {
		return array(
			'db' => $ini['db'],
			'memcache' => $ini['memcache'],
			'webroot' => $ini['webroot'],
		);
	}

	static private function WebRoot() {
		global $INI; 
		if (defined('WEB_ROOT')){
			return $INI;
		}
		
		/* validator */
		//Logs::setLog("PHP_SELF =>  ".$_SERVER['PHP_SELF']."|| SCRIPT_NAME=>".$_SERVER['SCRIPT_NAME']);
		$script_name = $_SERVER['PHP_SELF'] ? $_SERVER['PHP_SELF'] : $_SERVER['SCRIPT_NAME'];
		$m = explode('/', $script_name);
		if( $m ){
			define('WEB_ROOT', "/".$m[1]);
			return $INI;
		}
	}

	static private function BuildINI($ini) {
		$host = $_SERVER['HTTP_HOST'];
		$ini['system']['wwwprefix'] = "http://{$host}" . WEB_ROOT;
		$ini['system']['imgprefix'] = "http://{$host}" . WEB_ROOT;
		if(!$ini['system']['sitename']) {
			$ini['system']['sitename'] = '予約サイト';
		}
		if(!$ini['system']['sitetitle']) {
			$ini['system']['sitetitle'] = '予約サイト';
		}
		if(!$ini['system']['abbreviation']) {
			$ini['system']['abbreviation'] = '予約';
		}
		if(!$ini['system']['couponname']) {
			$ini['system']['couponname'] = '予約';
		}
		if(!$ini['system']['currency']) {
			$ini['system']['currency'] = '&#165;';
		}
		return $ini;
	}

	static public function GetThemeList() {
		$root = WWW_ROOT . '/static/theme';
		$dirs = scandir($root);
		$themelist = array( 'default'=> 'default',);
		foreach($dirs AS $one) {
			if ( strpos($one,'.') === 0 ) continue;
			$onedir = $root . '/' . $one;
			if ( is_dir($onedir ) ) $themelist[$one] = $one;
		}
		return $themelist;
	}

	static public function GetTemplateList() {
		$root = DIR_ROOT . '/template';
		$dirs = scandir($root);
		$templatelist = array( 'default' => 'default',);
		foreach($dirs AS $one) {
			if ( strpos($one,'.') === 0 ) continue;
			$onedir = $root . '/' . $one;
			if ( is_dir($onedir ) ) $templatelist[$one] = $one;
		}
		return $templatelist;
	}
}
